package com.ylabz.wavemail;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import java.util.logging.Logger;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class Mail {
   private static final Logger log = Logger.getLogger(Mail.class.getName());

   String subject;
   
   public String getSubject() {
      return subject;
   }

   public void setSubject(String subject) {
      this.subject = subject;
   }

   String sender;
   String body;
   
   public static List<Mail> getMessages() {
      List<Mail> l = new ArrayList<Mail>();
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imap");

      try {
         Session session = Session.getDefaultInstance(props, null);
         Store store = session.getStore("imaps");
         store.connect("imap.gmail.com", "testuser@plurchase.com", "test45user");
         Folder folder = store.getDefaultFolder();
         folder = folder.getFolder("inbox");
         folder.open(Folder.READ_ONLY);
         Message[] messages = folder.getMessages();
         
         for(Message m : messages) {
            Mail mail = new Mail();
            mail.subject = m.getSubject();
            mail.sender = ((InternetAddress) m.getFrom()[0]).getPersonal();
            l.add(mail);
         }
         
      } catch (NoSuchProviderException e) {
        log.warning(e.getMessage());
      } catch (MessagingException e) {
         log.warning(e.getMessage());
      } 
      
      return l;
   }
   
   public static void main(String [] args) {
      List<Mail> l = Mail.getMessages();
      
   }
   
}
